package org.javastudy.tang.lambda.example2;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p><em>Created on 2022/5/10 10:32 上午</em></p>
 * 用Lambda方式来实现
 * @author fuzhongtang
 * @since 1.0.1
 */
public class TestStudentByLamdba {
    public static void main(String[] args) {
        List<Student> stuList = Arrays.asList(
                new Student("张三","001",19),
                new Student("李四","005",22),
                new Student("王五","010",14),
                new Student("赵六","004",18),
                new Student("何琦","006",12)
        );
        sortByNo(stuList);
        System.out.println("age > 15");
        //查询所有年龄大于15的人
        filterCondition(stuList,(t)->t.getAge()>15);
        System.out.println("name like 张");
        //查询姓张的所有学生
        filterCondition(stuList,(t)->t.getName().startsWith("张"));
        System.out.println("all student");
        //查询所有学生对象，条件为true
        filterCondition(stuList, (t)->true);
    }

    //根据学号排序
    public static void sortByNo(List<Student> list) {
        Collections.sort(list,(s1, s2)->s1.getNo().compareTo(s2.getNo()));
    }

    public static void filterCondition(List<Student> stuList,Condition<Student> c) {
        for(Student stu:stuList) {
            if(c.compare(stu)) {
                System.out.println(""+stu.getNo()+" "+stu.getName()+" "+stu.getAge());
            }
        }
    }

    interface Condition<T> {
        public boolean compare(T t);
    }
}
